-- 特权

local Config = require("Config")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Util = require("common.Util")
local Lang = require("common.Lang")
local DB = require("common.DB")
local PresentExcel = require("excel.present")
local MailExcel = require("excel.mail")
local VipExcel = require("excel.vip")
local BuyExcel = require("excel.buy")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local MailManager = require("mail.MailManager")
local VipLogic = require("vip.VipLogic")
local Broadcast = require("broadcast.Broadcast")
local BuyLogic = require("topup.BuyLogic")
local KingWorldLogic = require("present.KingWorldLogic")
local BagLogic = require("bag.BagLogic")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local LuaMongo = _G.lua_mongo
local PanelDefine = require("broadcast.PanelDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local SceneHandler = require("scene.Handler")
local FuliExcel = require("excel.fuli")

-- buyid
CONFIG_ID1 = 1    -- 钻石基金
CONFIG_ID2 = 2    -- 钻石基金
CONFIG_ID3 = 3    -- 无限高抽

BUYSTATE0 = 0 -- 未购买
BUYSTATE1 = 1 -- 已购买1
BUYSTATE2 = 2 -- 已购买2
BUYSTATE3 = 3 -- 已购买3

STATE0 = 0 -- 不可领取
STATE1 = 1 -- 可以领取
STATE2 = 2 -- 已经领取

-- 获取购买状态
local function getState(db, lv, id, flag)
    if db.get[id] then
        return STATE2
    end

    -- 无限高抽
    if flag == CONFIG_ID3 then
        if db.flag == flag and lv > FuliExcel.growFund[id].lv then
            return STATE1
        end
    else
        -- 基金
        if db.flag2 == flag and lv >= FuliExcel.growFund[id].lv then
            return STATE1
        end
    end

    return STATE0
end

-- 获取礼包获奖范围
local function getRange(flag)
    local conf = FuliExcel.growFundDefine[1]
    return conf.range[flag], conf.buyID[flag]
end

function initDB(human)
    human.db.fundFlag = human.db.fundFlag or { flag = 0, get = { }, flag2 = 0 }
    human.db.fundFlag.flag2 = human.db.fundFlag.flag2 or 0
    return human.db.fundFlag
end


-- 特权查询
function query(human, type)
    local db = initDB(human)
    local flag = CONFIG_ID1
    if type == CONFIG_ID3 then
       flag = db.flag == 0 and CONFIG_ID3 or db.flag
    else
       flag = db.flag2 == 0 and CONFIG_ID1 or db.flag2
    end

    local rangeConfig, buyID = getRange(flag)
    if not rangeConfig then return end

    local bAllBuy = true
    for i = rangeConfig[1], rangeConfig[2] do
       local state = getState(db, human.db.lv, i, CONFIG_ID1)
       if state ~= STATE2 then
          bAllBuy = false
          break
       end
    end

    if type ~= CONFIG_ID3 and bAllBuy == true then
       flag = CONFIG_ID2
       rangeConfig, buyID = getRange(flag)
    end

    local msgRet = Msg.gc.GC_TEQUAN_ZSJIJIN_QUERY
    local net = msgRet.data
    local len = 0
    for i = rangeConfig[1], rangeConfig[2] do
        local config = FuliExcel.growFund[i]
        len = len + 1
        local netNode = net.lvRewardMsg[len]

        netNode.id = i
        netNode.lv = config.lv
        netNode.state = getState(db, human.db.lv, i, flag)
        if flag == CONFIG_ID3 then
            for j, h in ipairs(config.items) do
                Grid.makeItem(netNode.lvReward[j], h[1], h[2])
            end
            netNode.lvReward[0] = #config.items
        else
            Grid.makeItem(netNode.lvReward[1], ItemDefine.ITEM_ZUANSHI_ID, config.yuanbao)
            netNode.lvReward[0] = 1
        end
    end

    if type == CONFIG_ID3 then
        net.fundFlag = db.flag == flag and 1 or 0
    else
        net.fundFlag = db.flag2 == flag and 1 or 0
    end
    net.lvRewardMsg[0] = len
    BuyLogic.fontBuyItem(human, net.buyItem, buyID)
    Msg.send(msgRet, human.fd)
end

-- 钻石基金购买
function fund(human, funcID, buyConf, isFirst, cnt)
    local yyConfig = YunYingLogic.getFuncConfig(funcID)
    
    local buyType = buyConf.args[1]
    local rangeConfig, buyID = getRange(buyType)
    if not rangeConfig then return end
    if buyConf.id ~= buyID then return end

    if buyType == CONFIG_ID3 then
       if yyConfig.panelID ~= PanelDefine.PANEL_ID_3607 then return end 
    else
       if yyConfig.panelID ~= PanelDefine.PANEL_ID_3506 then return end 
    end

    local db = initDB(human)
    db.flag2 = db.flag2 or 0

    if buyType == CONFIG_ID3 then
        db.flag = CONFIG_ID3
        YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, PanelDefine.PANEL_ID_3607)
    else
        if buyType == CONFIG_ID1 or buyType == CONFIG_ID2 then  
            db.flag2 = buyType
            YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, PanelDefine.PANEL_ID_3506)
        end
    end

    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)

    query(human, buyType)
end

-- 获取基金奖励
function fundGet(human, id, type)
    local config = FuliExcel.growFund[id]
    if not config then return end

    local db = initDB(human)
    local flag = nil
    local rangeConfig = nil
    if type == CONFIG_ID3 then
        rangeConfig = getRange(db.flag)
        if not rangeConfig then return end
        if id < rangeConfig[1] or id > rangeConfig[2] then
            return
        end
        flag = db.flag
    else
        -- 基金判定
        rangeConfig = getRange(db.flag2)
        if not rangeConfig then return end
        if id < rangeConfig[1] or id > rangeConfig[2] then
            return
        end
        flag = db.flag2
    end

    -- 是否已领取
    if getState(db, human.db.lv, id, flag) ~= STATE1 then
        return
    end
    db.get[id] = 1

    BagLogic.cleanMomentItemList()
    if type == CONFIG_ID3 then
        for i = 1, #config.items do
            BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config.items[i][1], config.items[i][2])
        end

        for k, v in pairs(funcID) do
            YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3607)
        end
    else
        BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, ItemDefine.ITEM_ZUANSHI_ID, config.yuanbao)

        for k, v in pairs(funcID) do
            YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3506)
        end
    end
    BagLogic.addMomentItemList(human, "fundGet")

    query(human, type)
end


function isOpen(human,YYInfo,funcConfig)
    if not SceneHandler.canCharge(human) then
        return
    end
	return true
end

function isRed(human, YYInfo, funcConfig)
    local db = initDB(human)
    local rangeConfig = getRange(db.flag)
    if rangeConfig then
        for i = rangeConfig[1], rangeConfig[2] do
            local config = FuliExcel.growFund[i]
            if getState(db, human.db.lv, i, CONFIG_ID3) == STATE1 then
                return true
            end
        end
    end

    rangeConfig = getRange(db.flag2)
    if rangeConfig then
        for i = rangeConfig[1], rangeConfig[2] do
            local config = FuliExcel.growFund[i]
            if getState(db, human.db.lv, i, db.flag2) == STATE1 then
                return true
            end
        end
    end
end


function updateDaily(human)
    local db = initDB(human)
    local rangeConfig = getRange(db.flag)
    if rangeConfig then
        for i = rangeConfig[1], rangeConfig[2] do
            local config = FuliExcel.growFund[i]
            db.get[i] = nil
        end
    end
end

function onLevelUp(human, oldLv, newLv, id)
   YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_3506)
end